package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"order-module/order-api/client"
	"order-module/order-api/request"
	"order-module/order-rpc/proto/good"
)

func UserRegister(c *gin.Context) {
	var data request.User
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	register, err := client.UserRegister(c, &good.UserRegisterReq{
		Name:     data.Name,
		Phone:    data.Phone,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "用户注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "用户注册成功",
		"data": register,
	})
}

func GoodCreate(c *gin.Context) {
	var data request.Good
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到该参数",
			"data": nil,
		})
		return
	}
	create, err := client.GoodCreate(c, &good.GoodCreateReq{
		Name:    data.Name,
		Content: data.Content,
		Price:   float32(data.Price),
		Num:     int64(data.Num),
		Status:  int64(data.Status),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品下单失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "商品下单成功",
		"data": create,
	})
}

func GoodList(c *gin.Context) {
	var data request.Good
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到该参数",
			"data": nil,
		})
		return
	}
	list, err := client.GoodList(c, &good.GoodListReq{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品详情展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "商品详情展示成功",
		"data": list,
	})
}

func GoodOrder(c *gin.Context) {
	var data request.Order
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到此数据",
			"data": nil,
		})
		return
	}
	order, err := client.GoodOrder(c, &good.GoodOrderReq{
		Userid: data.GoodsId,
		Goodis: data.GoodsId,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "创建订单失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "创建订单成功",
		"data": order,
	})
}
